home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Miscellaneous
/
Headlines Code
/
Jamie’s PD Code ƒ
/
JMassMalloc.c
< prev
next >
Wrap
Text File
|
1992-08-03
|
3KB
|
155 lines
/*
* JMassMalloc.c
*
* "Jamie's Mass Malloc," version 1.0
*
* This is a really cheap-o version of malloc, in that you can't
* free up individual allocations, you have to free everything
* at once. I wrote it for my own personal use, it works for me,
* but I guarantee absolutely nothing about it.
*
* As I say, it's not too smart. On the bright side, though, it
* takes less than 400 bytes of object code, and it's pretty damn
* fast even without dropping into assembly language. :-)
*
* This code, including JMassMalloc.h, is in the public domain.
* That means I don't have any more rights to it than anyone else;
* you may freely use this code however you wish. If you modify
* it, though, please do me the courtesy of putting a different
* version number on it. I can be contacted at the AppleLink
* address "j.mccarthy" or the Internet address
* "j.mccarthy@applelink.apple.com".
*
*/
/******************************/
#include "JMassMalloc.h"
/******************************/
/* Never get a pointer less than 16K. */
#define kJMMMinAllocSize (16384L)
/* Word-align the allocations. */
#define kJMMAlignment (2)
/******************************/
typedef struct jmmAllocation {
struct jmmAllocation *next;
unsigned long used;
unsigned long size;
} jmmAllocationStruct;
/******************************/
jmmAllocationStruct *jmmHead;
/******************************/
Boolean jmmHeadHasFreeSpace(unsigned long nBytes);
void *jmmAllocateNewMem(unsigned long nBytes);
void *jmmReserveOldMem(unsigned long nBytes);
/******************************/
void setupJMM(void)
{
jmmHead = NULL;
}
void *malloc(unsigned long nBytes)
{
if (jmmHeadHasFreeSpace(nBytes)) return jmmReserveOldMem(nBytes);
else return jmmAllocateNewMem(nBytes);
}
void free(void *memToFree)
{
/*
* This implementation of malloc only lets you free up everything
* at once. Sorry. free() is only here for compatibility.
*/
}
void shutdownJMM(void)
{
register jmmAllocationStruct *theNext;
while (jmmHead != NULL) {
theNext = jmmHead->next;
DisposPtr(jmmHead);
jmmHead = theNext;
}
}
/******************************/
Boolean jmmHeadHasFreeSpace(unsigned long nBytes)
{
if (jmmHead == NULL) return FALSE;
else return ( (jmmHead->size - jmmHead->used) >= nBytes );
}
void *jmmAllocateNewMem(unsigned long nBytes)
{
jmmAllocationStruct *oldHead;
register jmmAllocationStruct *newHead;
register unsigned long actualAllocSize;
if (nBytes == 0) return NULL;
oldHead = jmmHead;
#if (kJMMAlignment > 1)
nBytes = ( (nBytes-1) / kJMMAlignment + 1 ) * kJMMAlignment;
#endif
actualAllocSize = nBytes;
if (actualAllocSize < kJMMMinAllocSize) actualAllocSize = kJMMMinAllocSize;
actualAllocSize += sizeof(jmmAllocationStruct);
newHead = (jmmAllocationStruct*) NewPtr(actualAllocSize);
if (newHead == NULL) return NULL;
jmmHead = newHead;
newHead->next = oldHead;
newHead->used = nBytes + sizeof(jmmAllocationStruct);
newHead->size = actualAllocSize;
return &(newHead[1]); // ugly, but it works
}
void *jmmReserveOldMem(unsigned long nBytes)
{
void *theReservedMem;
if (nBytes == 0) return NULL;
theReservedMem = & ( ((char*) jmmHead) [jmmHead->used] );
#if (kJMMAlignment > 1)
nBytes = ( (nBytes-1) / kJMMAlignment + 1 ) * kJMMAlignment;
#endif
jmmHead->used += nBytes;
return theReservedMem;
}